package com.frejoys.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.frejoys.common.aop.Authority;
import com.frejoys.common.aop.Authorization;
import com.frejoys.common.constant.CommonConstant;
import com.frejoys.common.dto.admin.*;
import com.frejoys.common.dto.admin.AddStoreCodeBatchDto;
import com.frejoys.common.dto.admin.AddStoreCodeDto;
import com.frejoys.common.dto.common.PageDto;
import com.frejoys.common.enums.EStoreCodType;
import com.frejoys.common.error.HttpStatus;
import com.frejoys.common.exception.GeneralException;
import com.frejoys.common.response.Result;
import com.frejoys.common.util.CacheKeyUtil;
import com.frejoys.common.util.RequestUtil;
import com.frejoys.common.vo.admin.StoreCodeDepositoryVo;
import com.frejoys.common.vo.admin.StoreCodeBatchListVo;
import com.frejoys.service.StoreCodeBatchService;
import com.frejoys.service.StoreCodeDepositoryService;
import com.frejoys.service.StoreCodeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "店铺支付码", description = "店铺支付码")
@RestController
@RequiredArgsConstructor
@RequestMapping(CommonConstant.routePrefixAdmin)
@Authorization(Authority.Private)
public class AdminStoreCodeController {

    private final StoreCodeBatchService storeCodeBatchService;

    private final StoreCodeDepositoryService storeCodeDepositoryService;

    private final RedissonClient redissonClient;

    private final StoreCodeService storeCodeService;

    @Operation(summary = "支付码生成")
    @PostMapping("/storeCode/addStoreCodeBatch")
    public Result addStoreCodeBatch(@RequestBody @Validated AddStoreCodeBatchDto dto) {
        // 默认1000个码
        dto.setNum(1000);

        RLock lock = redissonClient.getLock(CacheKeyUtil.storeCodeLockCacheKey(RequestUtil.userId()));

        if (lock.tryLock()) {
            try {
                storeCodeBatchService.addStoreCodeBatch(dto, RequestUtil.userId());
            } finally {
                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }
        } else {
            throw new GeneralException(HttpStatus.beingProcessed);
        }
        return Result.success();
    }

    @Operation(summary = "给店铺添加支付码")
    @PostMapping("/storeCode/addStoreCodeInfo")
    public Result addStoreCodeInfo(@RequestBody @Validated AddStoreCodeDto dto) {

        RLock lock = redissonClient.getLock(CacheKeyUtil.addStoreCodeLockCacheKey(RequestUtil.userId()));

        if (lock.tryLock()) {
            try {
                storeCodeService.addStoreCodeInfo(dto.getStoreId(),dto.getStoreCodeDepositoryId(), EStoreCodType.offLine.getValue());
            } finally {
                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }
        } else {
            throw new GeneralException(HttpStatus.beingProcessed);
        }

        return Result.success();
    }


    @Operation(summary = "换绑店铺支付码")
    @PostMapping("/storeCode/upStoreCode")
    public Result upStoreCode(@RequestBody @Validated AddStoreCodeDto dto) {

        RLock lock = redissonClient.getLock(CacheKeyUtil.addStoreCodeLockCacheKey(RequestUtil.userId()));

        if (lock.tryLock()) {
            try {
                storeCodeService.upStoreCode(dto.getStoreId(),dto.getStoreCodeDepositoryId(), EStoreCodType.offLine.getValue());
            } finally {
                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }
        } else {
            throw new GeneralException(HttpStatus.beingProcessed);
        }
        return Result.success();
    }

    @Operation(summary = "支付码仓库列表")
    @PostMapping("/storeCode/storeCodeDepositoryListInfo")
    public Result<IPage<StoreCodeDepositoryVo>> storeCodeDepositoryListInfo(@RequestBody @Validated StoreCodeAndNameDto dto) {
        return Result.success(storeCodeDepositoryService.storeCodeDepositoryListInfo(dto));
    }


    @Operation(summary = "店铺线下支付码生成批次列表")
    @PostMapping("/storeCode/storeCodeBatchListInfo")
    public Result<IPage<StoreCodeBatchListVo>> storeCodeBatchListInfo(@RequestBody @Validated PageDto dto) {
        return Result.success(storeCodeBatchService.storeCodeBatchListInfo(dto));
    }

    //TODO 待修改
//    @Operation(summary = "店铺支付码列表")
//    @PostMapping("/storeCode/storeAndCodeListInfo")
//    public Result<IPage<StoreCodeListVo>> storeAndCodeListInfo(@RequestBody @Validated StoreCodeListDto dto) {
//        return Result.success(storeCodeService.storeAndCodeListInfo(dto));
//    }


//    @Authorization(Authority.Public)
//    @Operation(summary = "根据支付码查询店铺id")
//    @PostMapping("/storeCode/storeIdInfo")
//    public Result<Integer> storeIdInfo(@RequestBody @Validated CodeDto dto) {
//        return Result.success(storeCodeService.storeIdInfo(dto));
//    }
}
